package com.estar.assistsurvy.adapter;

import java.util.ArrayList;
import java.util.List;

import com.bumptech.glide.Glide;
import com.estar.assistsurvey.entity.ImageVO;
import com.estar.assistsurvy.R;
import com.estar.assistsurvy.widget.ScaleView;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class PhotoviewAdapter extends PagerAdapter {

	private List<ImageVO> list;
	private List<View> views;
	private Context context;
	
	public PhotoviewAdapter(List<ImageVO> list, Context context) {
		super();
		this.list = list;
		this.context = context;
		views=new ArrayList<>();
		for(int i=0;i<list.size();i++){
			View view=LayoutInflater.from(context).inflate(R.layout.viewpager_photoview, null);
			views.add(view);
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return views.size();
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		// TODO Auto-generated method stub
		return arg0 == arg1;
	}
	
	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		// TODO Auto-generated method stub
		((ViewPager)container).removeView(views.get(position));
	}
	
	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		// TODO Auto-generated method stub
		View view=views.get(position);
		ImageVO image=list.get(position);
		ScaleView scaleview=(ScaleView) view.findViewById(R.id.scaleview);
		TextView photoIndex=(TextView) view.findViewById(R.id.photoIndex);
//		GlideUtiles.loadIntoUseFitWidth(context, image.getPicPath(), R.drawable.rrklogo, scaleview);
		Glide.with(context)
		.load(image.getPicPath())
		.fitCenter()
		.placeholder(R.drawable.rrklogo)
		.error(R.drawable.rrklogo)
		.into(scaleview);
		photoIndex.setText(position+1+"/"+list.size());
		container.addView(view);
		return views.get(position);
	}

}
